﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Zyg.SSO.Core.Business;
using Zyg.Utility.DBUtility;

namespace Zyg.SSO.Core.Providers.DALProviders
{
    public partial class SqlProvider : DALProvider
    {
        public override Zyg.SSO.Core.Business.AccountAppUser SelectAccountAppUser(Guid id)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@ID", SqlDbType.UniqueIdentifier, id);
            SqlDataReader reader = db.ExecuteSPReader("usp_SelectAccountAppUser");

            if (reader != null && !reader.IsClosed && reader.Read())
            {
                Business.AccountAppUser aau = new AccountAppUser();

                if (!reader.IsDBNull(1)) aau.Name = reader.GetString(1);
                if (!reader.IsDBNull(2)) aau.Password = reader.GetString(2);
                if (!reader.IsDBNull(3)) aau.DateCreated = reader.GetDateTime(3);
                if (!reader.IsDBNull(4)) aau.AccountAppID = reader.GetGuid(4);

                reader.Close();

                return aau;
            }
            else
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();

                return null;
            }
        }

        public override void InsertAccountAppUser(Zyg.SSO.Core.Business.AccountAppUser accoutAppUser)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@ID", SqlDbType.UniqueIdentifier, accoutAppUser.Id);
            db.AddParameter("@Name", SqlDbType.VarChar, accoutAppUser.Name);
            db.AddParameter("@Password", SqlDbType.VarChar, accoutAppUser.Password);
            db.AddParameter("@DateCreated", SqlDbType.DateTime, accoutAppUser.DateCreated);
            db.AddParameter("@AccountAppID", SqlDbType.UniqueIdentifier, accoutAppUser.AccountAppID);

            db.ExecuteSP("usp_InsertAccountAppUser");
        }

        public override void DeleteAccountAppUser(Zyg.SSO.Core.Business.AccountAppUser accoutAppUser)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@ID", SqlDbType.UniqueIdentifier, accoutAppUser.Id);

            db.ExecuteSP("usp_DeleteAccountAppUser");
        }

        public override void UpdateAccountAppUser(Zyg.SSO.Core.Business.AccountAppUser accoutAppUser)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@ID", SqlDbType.UniqueIdentifier, accoutAppUser.Id);
            db.AddParameter("@Name", SqlDbType.VarChar, accoutAppUser.Name);
            db.AddParameter("@Password", SqlDbType.VarChar, accoutAppUser.Password);
            db.AddParameter("@DateCreated", SqlDbType.DateTime, accoutAppUser.DateCreated);
            db.AddParameter("@AccountAppID", SqlDbType.UniqueIdentifier, accoutAppUser.AccountAppID);

            db.ExecuteSP("usp_UpdateAccountAppUser");
        }

        public override IList<AccountAppUser> FillAccountAppUsers(AccountApp accountApp)
        {
            SqlServerUtility db = new SqlServerUtility();

            IList<AccountAppUser> aaus = new List<AccountAppUser>();

            SqlDataReader reader;

            string accountAppId = accountApp.Id.ToString();

            db.AddParameter("@WhereCondition", SqlDbType.VarChar, " [AccountAppID] = '" + accountAppId + "'");

            reader = db.ExecuteSPReader("usp_SelectAccountAppUsersDynamic");

            if (reader != null)
            {
                while (reader.Read())
                {
                    Business.AccountAppUser aau = new AccountAppUser();

                    if (!reader.IsDBNull(0)) aau.Id = reader.GetGuid(0);
                    if (!reader.IsDBNull(1)) aau.Name = reader.GetString(1);
                    if (!reader.IsDBNull(2)) aau.Password = reader.GetString(2);
                    if (!reader.IsDBNull(3)) aau.DateCreated = reader.GetDateTime(3);
                    if (!reader.IsDBNull(4)) aau.AccountAppID = reader.GetGuid(4);

                    aau.MarkOld();

                    aaus.Add(aau);
                }

                reader.Close();
            }

            return aaus;
        }

        public override void DeleteAccountAppUsers(AccountApp accountApp)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@AccountAppID", SqlDbType.UniqueIdentifier, accountApp.Id);

            db.ExecuteSP("usp_DeleteAccountAppUsersByAccountAppID");
        }
    }
}
